﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.ChildWindows;

namespace Baosight.ZNH.ZMCL.UI
{
    /// <summary>
    /// 安全隐患月报列表页面
    /// </summary>
    /// Title:ZMCR1
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2011-05-30  高强   [创建]
    ///  
    public partial class ZMCLR1 : EFViewCmes
    {
        private AddNewReportBlock block = new AddNewReportBlock();//构造前台下拉列表
        private string actionType;
        public ZMCLR1()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 新增/修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.Grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Platform.Core.EIInfo initData)
        {
            initData.AddBlock(block.getReportYear(true));//构造年份选择下拉框
            initData.AddBlock(block.getReportMoth(true));//构造月份选择下拉框
            base.Initialize(initData);
        }

        /// <summary>
        /// 查询按钮按下的操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnQueryButtonClick(object sender, RoutedEventArgs e)
        {
            if (this.fdYear.Value == null && this.fdMoth.Value == null)
            {
                this.fdReportDate.Value = null;
                base.OnQueryButtonClick(sender, e);
            }
            else if (this.fdYear.Value == null)
            {
                DialogParameters param = new DialogParameters();
                param.CancelButtonContent = "取消";
                param.OkButtonContent = "确定";
                param.Content = "请输入年份!";
                param.Header = "提示框";
                EFWindow.Alert(param);
            }
            else if (this.fdMoth.Value == null)
            {
                DialogParameters param = new DialogParameters();
                param.CancelButtonContent = "取消";
                param.OkButtonContent = "确定";
                param.Content = "请输入月份!";
                param.Header = "提示框";
                EFWindow.Alert(param);
            }
            else
            {
                this.fdReportDate.Value = this.fdYear.Value.ToString() + this.fdMoth.Value.ToString();
                base.OnQueryButtonClick(sender, e);
            }
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnMAKEREPORTButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "安全月报-新增", "insert", null, sender, e);
        }

        /// 预览安全隐患日报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PerviewButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hyperlink = sender as HyperlinkButton;
            hyperlink.IsEnabled = false;
            base.TempHyperlink = hyperlink;
            EIRow row = (hyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            Dictionary<string, object> args = new Dictionary<string, object>();
            args.Add("fdReportId", row["fdKeyId"]);//传递隐患月报主键
            HttpServiceAgent agent = new HttpServiceAgent();
            EIInfo eiInfo = new EIInfo();
            eiInfo.SetAttributes(args);
            eiInfo.Attributes.Add(EIConstants.ServiceNameKey, "ZMCLR2");
            eiInfo.Attributes.Add(EIConstants.MethodNameKey, "queryAll");
            agent.CallService(eiInfo, OnPerviewQueryWorkCompleted);
        }

        /// <summary>
        /// 预览安全隐患日报的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnPerviewQueryWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                if (e.EIInfo.GetBlock("result").GetRowCount() == 0)
                {
                    base.OpenEFWindowAlert("当前报表无数据，请确认！", true);
                    //超链接生效
                    if (base.TempHyperlink != null)
                    {
                        base.TempHyperlink.IsEnabled = true;
                        base.TempHyperlink = null;
                    }
                    return;
                }
            }
            else
            {
                EFWindow.Alert(e.EIInfo.GetMsg());
            }
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closed(object sender, WindowClosedEventArgs e)
        {
            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 发布按钮的执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPUBLISHButtonClick(object sender, RoutedEventArgs e)
        {
            this.actionType = "publish";
            base.OnModifyButtonClick("updateStatus", "发布", sender, e);
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            this.actionType = "delete";
            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 构造弹出新增月报页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        /// <param name="dialogType"></param>
        /// <param name="arg"></param>
        public override void OpenPopup(EIInfo eIInfo, string title, string dialogType, string arg)
        {
            AddNewMothReport addNewMothReport = new AddNewMothReport();//构造一个新的修改页面
            addNewMothReport.EIDataContext = new Platform.Core.UI.EIDataContext();
            addNewMothReport.fdStatus.Value = "未发布";
            addNewMothReport.fdType.Value = "1";
            addNewMothReport.fdTitle.Value = "安全隐患月报";
            eIInfo.AddBlock(block.getReportYear(false));
            eIInfo.AddBlock(block.getReportMoth(false));
            eIInfo.AddBlock(block.getReportType());//过滤类型
            eIInfo.AddBlock(block.getReportStauts());
            addNewMothReport.EIDataContext.Update(eIInfo);
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = title == null ? "" : title;//设置Title
            _popup.Content = addNewMothReport;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(ParentInsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 主表弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ParentInsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.Grid.Refresh();
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }

        /// <summary>
        /// 删除的判断
        /// </summary>
        /// <param name="rowList"></param>
        public override bool BeforeMethod(List<EIRow> rowList)
        {
            bool DeleteFlag = true;
            if (this.actionType == "delete")
            {
                foreach (var row in rowList)
                {
                    if (row["fdStatus"].Equals("已发布"))
                    {
                        base.OpenEFWindowAlert("勾选的记录包含已发布记录，不能删除，请重新选择需要删除的记录!", true);
                        DeleteFlag = false;
                        break;
                    }
                }
            }
            else if (this.actionType == "publish")
            {
                foreach (var row in rowList)
                {
                    if (row["fdStatus"].Equals("已发布"))
                    {
                        base.OpenEFWindowAlert("勾选的记录包含已发布的记录，不能重复发布，请重新选择需要未发布的记录!", true);
                        DeleteFlag = false;
                        break;
                    }
                }
            }
            return DeleteFlag;
        }

        /// <summary>
        /// 选中主表一条记录，显示该条记录的从表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (this.Grid.SelectedItem != null)
            {
                EIRow _row = (this.Grid.SelectedItem as UIBaseEntity).GetEIRow();
                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add("fdReportId", _row["fdKeyId"].ToString());
                args.Add("fdRiskDate", _row["fdReportDate"].ToString());
                args.Add("fdStatus", _row["fdStatus"].ToString());
                args.Add("fdType", _row["fdType"].ToString());
                this.container.LoadView<EFCustomViewContainerPanel>("ZMCLR2", args);
            }
            else
            {
                this.container.ClearValue(EFCustomViewContainerPanel.ContentProperty);//清空报表区域内容
            }
        }
    }
}
